﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DTOMall
{
    public class DBTool
    {
        private static string strConStr = ConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
        private static SqlDataReader datareader;
        #region DB Access Functions
        static SqlConnection con;
        public string ConnectingString
        {
            get
            {
                if (!strConStr.Equals(""))
                    return strConStr;
                else
                    return null;
            }
            set { strConStr = value; }
        }
        public static SqlConnection GetConnection()
        {
            con = new SqlConnection(strConStr);
            con.Open();
            return con;
        }
        public static void CloseConnection()
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        public static DataTable GetTable(SqlCommand cmd)
        {
            try
            {
                if (cmd.Connection != null)
                {
                    using (var ds = new DataSet())
                    {
                        using (var da = new SqlDataAdapter())
                        {
                            da.SelectCommand = cmd;
                            da.Fill(ds);
                            return ds.Tables[0];
                        }
                    }
                }
                else
                {
                    using (SqlConnection conn = GetConnection())
                    {
                        using (var ds = new DataSet())
                        {
                            using (var da = new SqlDataAdapter())
                            {
                                da.SelectCommand = cmd;
                                da.SelectCommand.Connection = conn;
                                da.Fill(ds);
                                return ds.Tables[0];
                            }
                        }
                    }
                }
            }
            finally
            {
            }
        }

        public static SqlDataReader ExecuteReader(SqlCommand cmd)
        {
            try
            {
                if (cmd.Connection != null)
                {
                    return cmd.ExecuteReader();
                }
                else
                {
                    using (SqlConnection conn = GetConnection())
                    {
                        cmd.Connection = conn;
                        datareader = cmd.ExecuteReader();
                        return cmd.ExecuteReader();
                    }
                }
            }
            finally
            {
            }
        }


        public static DataSet GetDsData(SqlCommand cmd)
        {
            try
            {
                if (cmd.Connection != null)
                {
                    using (var ds = new DataSet())
                    {
                        using (var da = new SqlDataAdapter())
                        {
                            da.SelectCommand = cmd;
                            da.Fill(ds);
                            return ds;
                        }
                    }
                }
                else
                {
                    using (SqlConnection conn = GetConnection())
                    {
                        using (var ds = new DataSet())
                        {
                            using (var da = new SqlDataAdapter())
                            {
                                da.SelectCommand = cmd;
                                da.SelectCommand.Connection = conn;
                                da.Fill(ds);
                                return ds;
                            }
                        }
                    }
                }
            }
            finally
            {
            }
        }

        public static DataTable GetTable(string sql)
        {
            try
            {
                using (SqlConnection conn = GetConnection())
                {
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = sql;
                        using (var ds = new DataSet())
                        {
                            using (var da = new SqlDataAdapter())
                            {
                                da.SelectCommand = cmd;
                                da.SelectCommand.Connection = conn;
                                da.Fill(ds);
                                return ds.Tables[0];
                            }
                        }
                    }
                }
            }
            finally
            {
            }
        }

        public static void ExeCuteNonquery(SqlCommand cmd)
        {
            try
            {
                using (SqlConnection conn = GetConnection())
                {
                    cmd.Connection = conn;
                    cmd.ExecuteNonQuery();
                }
            }
            finally
            {
            }
        }
        public static void ExeCuteNonquery(string sql)
        {
            try
            {
                using (SqlConnection conn = GetConnection())
                {
                    var cmd = new SqlCommand(sql, conn);
                    cmd.ExecuteNonQuery();
                }
            }
            finally
            {
            }
        }
        public static SqlDataReader ExecuteReader(string sql)
        {
            try
            {
                var cmd = new SqlCommand(sql, GetConnection());
                return cmd.ExecuteReader();
            }
            finally
            {
            }
        }

        public static string ExecuteScalar(string sql)
        {
            try
            {
                using (SqlConnection conn = GetConnection())
                {
                    var cmd = new SqlCommand(sql, conn);
                    return cmd.ExecuteScalar().ToString();
                }
            }
            finally
            {
            }
        }

        public object ExecuteScalar(SqlCommand cmd)
        {
            try
            {
                using (SqlConnection conn = GetConnection())
                {
                    cmd.Connection = conn;
                    return cmd.ExecuteScalar();
                }
            }
            finally
            {
            }
        }
        #endregion
    }
}
